#!/bin/sh
set +x
mntpt=$1
orasid=$2
archmnt=$3
ouser=$4
tstamp=$5
dbfsPath=$6
flag=$7

if [ "$#" -lt 5 ]; then
 echo "This script needs min 5 input parameter"
 echo "usage: recoverRman_tstamp.sh <Disk Group Name> <database sid name> <Log Backup location mount path> <Oracle User> <timestamp yyyy-mm-dd..hh24:mi:ss>"
exit 1
fi

scriptsRoot=${LB_ROOT}
if [ -z "$scriptsRoot" ];then
    echo "missing env LB_ROOT"
    exit 1
fi

BASEDIR=$(dirname $0)
echo $BASEDIR
cd $BASEDIR

export ORACLE_SID=$orasid

. ${scriptsRoot}/oracle/oracleclone/OracleAppMountComFunc.sh
dbrecover="dbrecover_"$orasid".txt"
cstrfile="catalogArchivelog_"$orasid".txt"
cstrfiletmp="catalogArchivelogTmp_"$orasid".txt"
change_own $ouser $dbrecover

if [ -f $cstrfile ]; then
 rm -f $cstrfile
fi

if [ -z "$rmanchannels" ]; then
rmanchannels=4;
fi

allocatech=
releasech=
i=0
while [ $i -lt "$rmanchannels" ]; do
if [ -z "$allocatech" ]; then
allocatech="allocate channel d$i device type disk;"
releasech="release channel d$i;"
else
allocatech=$allocatech"allocate channel d$i device type disk;"
releasech=$releasech"release channel d$i;"
fi
i=`expr $i + 1`
done

if [ ! -f "$dbfsPath" ]; then
  roTBS=''
else
 roTBS=`cat $dbfsPath`
fi

if [ ! -z "$roTBS" ]; then
recoverCMD="skip tablespace $roTBS"
else
recoverCMD=""
fi 

echo "******* recovering database *********** "
export NLS_DATE_FORMAT="yyyy-mm-dd..hh24:mi:ss"
export NLS_DATE_LANGUAGE=ENGLISH
export NLS_LANG=''

if [ ! -z "$archmnt" ]; then
     dirs=`ls -d $archmnt*`
     cstr=""
     for d in ${dirs}; do
         archmntpt=$d"/archivelog/"
         find $archmntpt -mtime -"$imageLogOffset" -type f >> $cstrfile
     done

     sed -e "s/^/catalog BACKUPPIECE '/" $cstrfile > $cstrfiletmp
     mv $cstrfiletmp $cstrfile
     sed  "s/$/' ;/" $cstrfile > $cstrfiletmp
     mv $cstrfiletmp $cstrfile
     if [ "$flag" != "no" ]; then
        cstr=`cat $cstrfile`
     else
        cstr=''
     fi

   if [ ! -z "$tstamp" ]; then
     if [ "$pdbcnt" -gt "0" ] && [ "$ischild" = "true" ]; then
    
        rmancmd="run
        {
	$allocatech
        catalog start with '$mntpt/archivelog' noprompt;
        $cstr 
	recover database until time \"to_date('$tstamp','yyyy-mm-dd..hh24:mi:ss')\" skip tablespace \"pdb\$seed\":SYSTEM, \"pdb\$seed\":SYSAUX;
	$releasech
        }"
      else
        rmancmd="run
        {
	$allocatech
        catalog start with '$mntpt/archivelog' noprompt;
        $cstr 
        recover database until time \"to_date('$tstamp','yyyy-mm-dd..hh24:mi:ss')\" $recoverCMD;
	$releasech
        }"
     fi

retv1=`su $ouser -c "$ORACLE_HOME/bin/rman target / log=$dbrecover append " << EOF
                 sql "alter session set NLS_DATE_FORMAT=''yyyy-mm-dd..hh24:mi:ss''";
                 sql "alter session set NLS_DATE_LANGUAGE=''ENGLISH''";
                 $rmancmd
                 exit
EOF`

   else
	rmancmd="run
        {
	$allocatech
        catalog start with '$mntpt/archivelog' noprompt;
        $cstr 
        recover database $recoverCMD;
	$releasech
        }"
	
	retv2=`su $ouser -c "$ORACLE_HOME/bin/rman target / log=$dbrecover append " << EOF
                 $rmancmd
                 exit
EOF`

   fi
else
        rmancmd="run
        {
	$allocatech
        catalog start with '$mntpt/archivelog' noprompt;
	recover database $recoverCMD;
	$releasech
        }"
	
	retv3=`su $ouser -c "$ORACLE_HOME/bin/rman target / log=$dbrecover append " << EOF
                 $rmancmd
                 exit
EOF`

fi
if [ -f $cstrfile ]; then
 rm -f $cstrfile
fi
exit 0

